﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        public byte[] getfilebytes(string filename)
        {
            if (filename == "")
                return null;
            try
            {
                FileStream filestream = new FileStream(filename, FileMode.Open,
                FileAccess.Read);
                BinaryReader binaryreader = new BinaryReader(filestream);
                byte[] filebytes = binaryreader.ReadBytes((int)filestream.Length);
                binaryreader.Close();
                filestream.Close();
                return filebytes;
            }
            catch
            {
                return null;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    byte[] picbinary = getfilebytes(openfile.FileName);
                    //第一步：打开图片文件，获得比特流
                    MemoryStream mempicstream = new MemoryStream(picbinary);
                    //第二步：将比特流存储在内存工作流中
                    pictureBox1.Image = Image.FromStream(mempicstream);
                    //第三步：加载内存流到图片控件
                    mempicstream.Dispose();
                    mempicstream.Close();
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("读取图片出错，可能的问题是：" + Convert.ToString(m), "错误提示");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = Graphics.FromImage(pictureBox1.Image);
            Font f=new Font ("隶书",80,FontStyle.Italic);
            Pen p=new Pen (Color .OrangeRed);
            g.DrawString ("花开花落",f,p .Brush ,0,0);
            p.Dispose ();
            g.Dispose ();
        }
    }
}
